#include <func.h>

#define MAX_PATH_LEN 1024

int cmd_remove(int netfd, const char* usrname, const char* path){
    int sret;
    char filePath[MAX_PATH_LEN];
    char absPath[MAX_PATH_LEN];

    if (path[0] == '.'){
        //相对路径，直接转换为绝对路径
        realpath(path, absPath);
        strcpy(filePath, absPath);
    } else {
        //绝对路径，直接拼接
        sprintf(filePath, "%s/%s", usrname, path);
    }

    if (access(filePath, F_OK) == -1){
        char msg[] = "File not found\n";
        sret = send(netfd, msg, strlen(msg), 0);
        ERROR_CHECK(sret, -1, "send");
        return -1;
    }

    if (unlink(filePath) == -1){
        perror("unlink");
        char msg[] = "Server error\n";
        sret = send(netfd, msg, strlen(msg), 0);
        ERROR_CHECK(sret, -1, "send");
        return -1;
    }

    char msg[] = "delete done\n";
    sret = send(netfd, msg, strlen(msg), 0);
    ERROR_CHECK(sret, -1, "send");
    return 0;
}

